package 蓝桥算法;

import 二叉树.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class day16 {
    public List<Integer> preorderTraversal(TreeNode root){//前序遍历
         List<Integer>result=new ArrayList<>();
         preorder(root,result);
         return  result;
    }
    public void preorder(TreeNode root,List<Integer> result){
        if(root==null){
            return;
        }
        result.add(root.val);
        preorder(root.left,result);
        preorder(root.right,result);
    }
    public List<Integer> postorder(TreeNode root){//中序遍历
        List<Integer>result=new ArrayList<>();
        postorder(root,result);
        return result;
    }
    public void postorder(TreeNode root,List<Integer> result){
        if(root==null){
            return;
        }
        postorder(root.left,result);
        result.add(root.val);
        postorder(root.right,result);
    }
    public List<Integer> lateorder(TreeNode root){//后序遍历
        List<Integer> result=new ArrayList<>();
        laterorder(root,result);
        return result;
    }
    public void laterorder(TreeNode root,List<Integer>result){
        if(root==null){
            return;
        }
        laterorder(root.left,result);
        laterorder(root.right,result);
        result.add(root.val);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(
                new TreeNode(
                        new TreeNode(4),
                        2,
                        new TreeNode(5)
                ),
                1,
                new TreeNode(
                        new TreeNode(6),
                        3,
                        new TreeNode(7)
                )
        );
        day16 a=new day16();
        List<Integer> result=a.preorderTraversal(root);
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }

}

